﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ylh.Data;
using Ylh.Enums;
using Ylh.ElvModel;
using System.Data;

namespace Ylh.ElvBLL
{
    class RoleElvBLL
    {
        /// <summary>
        /// 执行插入角色,返回插入后的的角色ID
        /// 此方法需要重写底层
        /// </summary>
        /// <param name="ConStr">指定的数据库</param>
        /// <param name="RoleName">角色名称</param>
        /// <param name="Remark">角色备注</param>
        /// <param name="Remark">创建人</param>
        /// <returns>角色ID</returns>
        public int InsertRole(string ConStr, string RoleName, string Remark, string Creater, string ProtectCount)
        {
            //string sql = @"insert INTO TbRole(RoleName,Remark,Creater,ProtectCount) VALUES(@RoleName,@Remark,@Creater,@ProtectCount) SELECT @@identity";
            //SqlParameter[] pre = new SqlParameter[] 
            //{
            //     new SqlParameter ("@RoleName",RoleName),
            //  new SqlParameter ("@Remark",Remark),
            //  new SqlParameter("@Creater",@Creater),
            //   new SqlParameter("@ProtectCount",ProtectCount)
            // };
            //DataTable dt = SqlHelper.ExcuteQuery(ConStr, sql, pre, CommandType.Text);
            //return Convert.ToInt32(dt.Rows[0][0]);

            return -1;
        }
        /// <summary>
        /// 更新角色信息
        /// </summary>
        /// <param name="ConStr">指定的数据库</param>
        /// <param name="RoleName">角色名称</param>
        /// <param name="Remark">角色备注</param>
        /// <param name="RoleID">角色编号</param>
        /// <returns>是否更新成功</returns>
        public bool UpdateRole(String ConStr, string RoleName, string Remark, decimal RoleID, string ProtectCount)
        {
            TbRole role = new TbRole();
            role.RoleID = RoleID.ToString();
            role.RoleName = RoleName.ToString();
            role.Remark = Remark.ToString();
            role.ProtectCount = ProtectCount.ToString();
            role.RoleID = RoleID.ToString();
            return TbRole.Update(role, ConStr);
        }



        /// <summary>
        /// 判断该名称是否存在
        /// </summary>
        /// <param name="ConStr">指定的数据库</param>
        /// <param name="RoleName">角色名称</param>
        /// <returns></returns>
        public bool IsExistsRole(string ConStr, string RoleName)
        {
            TbRole role = new TbRole();
            role.RoleName = RoleName;

            SqlWhere where = new SqlWhere();
            where.Info = role;
            return TbRole.Select(where, ConStr).Rows.Count > 0;
        }


        /// <summary>
        /// 获取对应的角色信息
        /// 如果RoleID等于-1是获取所有信息
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="RoleID"></param>
        /// <returns></returns>
        public DataTable GetRoleInFo(string ConStr, decimal RoleID)
        {
            TbRole role = new TbRole();
            role.RoleID = RoleID.ToString();

            SqlWhere where = new SqlWhere();
            where.Info = role;
            return TbRole.Select(where, ConStr);


        }


        /// <summary>
        /// 根据编号删除角色
        /// 顺便删除编号下面已经有的权限
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="RoleID">角色编号</param>
        /// <returns></returns>
        public bool DeleteRoleByRoleID(string ConStr, string RoleID)
        {
            TbComRole ComRole = new TbComRole();
            OQLWhere where = new OQLWhere(ComRole);
            where.AND(ComRole.RoleID, OperateType.等于, RoleID);

            TbRole role = new TbRole();
            OQLWhere where2 = new OQLWhere(role);
            where2.AND(role.RoleID, OperateType.等于, RoleID);
            return TbRole.Delete(where2, ConStr) && TbComRole.Delete(where, ConStr);
        }

        /// <summary>
        /// 判断角色是否已经有员工使用了
        /// </summary>
        /// <param name="ConStr"></param>
        /// <param name="RoleID">角色编号</param>
        /// <returns></returns>
        public bool IsUseByStaff(String ConStr, string RoleID)
        {
            TbStaff Staff = new TbStaff();
            Staff.RoleID = RoleID;
            SqlWhere where = new SqlWhere();
            where.Info = Staff;
            return TbStaff.Select(where, ConStr).Rows.Count > 0;
        }


        public int GetProtectCount(string ConStr, string RoleID)
        {
            TbRole role = new TbRole();
            OQLWhere where = new OQLWhere(role);
            where.AND(role.RoleID, OperateType.等于, role.RoleID);
            where.SelectColumn = new ColumnOpera(role)
                .CreateSelectColumn(role.RoleID);
            DataTable dt = TbRole.Select(where, ConStr);
            return Convert.ToInt32(dt.Rows[0][0]);
        }
    }
}
